from tqdm import tqdm

from src.base import Consts


class InitNegativeInfo:
    def __init__(self, logger, localDb, dataCenterDb, fcdb, es, enterpriseList):
        self.logger = logger
        self.localDb = localDb
        self.dataCenterDb = dataCenterDb
        self.fcdb = fcdb
        self.es = es
        self.enterpriseList = enterpriseList

    # 非标违约数量
    def nonStandardDefault(self, creditCode):
        sql = """
            SELECT count(1) as total from `data-center`.base_non_standard_default bnsd 
            where credit_code =%s 
        """
        return self.dataCenterDb.QueryOne(sql, (creditCode,))['total']

    # qe评级下调
    def qeRatingDown(self):
        sql = """
        
        """
        return None

    # 外部评级下调
    def publicRankDown(self):
        return None

    # 负面信息初始化
    def update(self):
        for item in tqdm(self.enterpriseList, desc="es数据库-负面信息保存/更新中...", position=0):
            creditCode = item['_source']['credit_code']
            # qe级别下调
            body = {
                "doc": {
                    "negative_info": {
                        "nonStandardCount": self.nonStandardDefault(creditCode)
                    }
                }
            }
            self.es.update(index=Consts.es_enterpriseList_index, doc_type="test-type", id=item['_id'], body=body)
